import pyautogui
import time

#滚动条方法  '-1280,1492,298,raca
def runScroll(time,scroll):
  n = 0
  print("raca滚动",time)
  if time == 1:
    pyautogui.moveTo(int(scroll[1]), int(scroll[2]))
    pyautogui.click()
    print("第1次不滚动")
  elif time > 7:
    print("第7次开始不滚动")
    if time == 8:
      n = 1
    elif time == 9:
      n = 2
    elif time == 10:
      n = 3
    pyautogui.moveTo(int(scroll[1]), int(scroll[2]) + (175*n))
    pyautogui.click()
  elif time == 2:
    pyautogui.scroll(-2500)
    pyautogui.click()
    print("第2次滚动")
  else:
    pyautogui.moveTo(int(scroll[1]), int(scroll[2]))
    pyautogui.scroll(int(scroll[0]))
    pyautogui.click()
    print("其他次数滚动", "滚动", int(scroll[0]))


#鼠标移动方法  '-1280,1492,298,raca
def runMove(time,scroll):
  n = 0
  print("raca元兽移动选择",time)
  if time == 1:
    pyautogui.moveTo(int(scroll[0]), int(scroll[1]))
    pyautogui.click()
    print("第1次不移动")
  elif time <= 5:
    pyautogui.moveTo(int(scroll[0]), int(scroll[1]) + (160*(time-1)))
    pyautogui.click()
    print("前5只元兽滚动")
  else:
    if time == 6:
      n = 0
    elif time == 7:
      n = 1
    elif time == 8:
      n = 2
    elif time == 9:
      n = 3
    elif time == 10:
      n = 4
    pyautogui.moveTo(int(scroll[0]) + 164, int(scroll[1]) + (160*n))
    pyautogui.click()
    print("后5只元兽滚动")

#选择战场
def batleSelect():
  #根据图标找到封锁的战场
  data = []
  pyautogui.locateAllOnScreen('raca/lock.png')
  for pos in pyautogui.locateAllOnScreen(r'D:\project\python_project\waterRPA\raca\lock.png'):
    data.append(list(pos))

  #按最左排序,选择该元兽可以进去的最高战场
  data = sorted(data)
  data = data[0]

  #进入战场 left=0, top=0, width=300, height=400
  pyautogui.moveTo(data[0]-480 , data[1])
  pyautogui.click()

  print("选择战场")


#玩游戏
#@with_goto
def battle():
  print("开始玩游戏")

  #元兽总数为10个
  metamon = 10
  for num in range(0, int(metamon)):
    print("第",num+1,"只元兽开始玩游戏")

    #若为True 进行下一次循环
    flag = False
    accept_challenge = False

    #点击选择元兽按钮
    while True:
      location = pyautogui.locateCenterOnScreen("raca/metamon.png", confidence=0.9)
      if location is not None:
        pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
        break
      print("未找到选择元兽按钮图片,0.1秒后重试")
      time.sleep(0.1)

    #选择元兽
    runMove(num+1, ['237','297'])

    #接受挑战
    while True:
      location = pyautogui.locateCenterOnScreen("raca/accept-challenge.png", confidence=0.9)
      if location is not None:
        pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
        break
      else:
        #若发现已经接受了跳转,点击返回
        location = pyautogui.locateCenterOnScreen("raca/return.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
          accept_challenge = True
          break
        print("未找到返回按钮图片 - 元兽列表(已接受跳转准备点击返回按钮), 0.1秒后重试")
        break
      time.sleep(0.1)

    #停止1秒，防止抓取图片点击图片失效
    time.sleep(3)

    #返回(如果选择元兽的时候发现已经是已选择状态了，那这一步的返回过掉)
    if accept_challenge == False:
      while True:
        location = pyautogui.locateCenterOnScreen("raca/return.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
          break
        print("未找到返回按钮图片,0.1秒后重试")
        time.sleep(0.1)

    #停止1.5秒，防止抓取图片点击图片失效
    #time.sleep(0.5)

    #点击battle图标
    while True:
      location = pyautogui.locateCenterOnScreen("raca/battle.png", confidence=0.9)
      if location is not None:
        pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
        break
      print("未找到battle按钮图片,0.1秒后重试")
      time.sleep(0.1)

    #判断是否已battle
    while True:
      location = pyautogui.locateCenterOnScreen("raca/x.png", confidence=0.9)
      if location is not None:
        pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
        flag = True
        break
      else:
        print("未找到X按钮图片,0.1秒后重试")
        break
      time.sleep(0.1)

    #检查flag
    if flag == True:
      continue

    #选择battlle场次
    batleSelect()

    #开始battle
    #每只元兽玩20盘
    n = 1
    while n < 21:
      print("第",n,"次玩游戏")
      #选择随机战斗
      while True:
        location = pyautogui.locateCenterOnScreen("raca/randommatch.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0, duration=0.1, button="left")
          break
      time.sleep(0.1)

      #点击确认
      while True:
        location = pyautogui.locateCenterOnScreen("raca/confirm.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0, duration=0.1, button="left")
          break
      time.sleep(0.1)

      #跳过
      while True:
        location = pyautogui.locateCenterOnScreen("raca/skip.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0, duration=0.1, button="left")
          break
      time.sleep(0.1)

      #游戏结束后点击确定
      while True:
        location = pyautogui.locateCenterOnScreen("raca/gameconfirm.png", confidence=0.9)
        if location is not None:
          pyautogui.click(location.x, location.y, clicks=1, interval=0, duration=0.1, button="left")
          break
      time.sleep(0.1)
      n+=1

    time.sleep(1)
    #返回
    while True:
        location = pyautogui.locateCenterOnScreen("raca/return2.png", confidence=0.9)
        if location is not None:
          print("返回按钮图片1 - 游戏结束",location.x, location.y)
          pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
          break
        print("未找到返回按钮图片1 - 游戏结束,0.1秒后重试")
        time.sleep(0.1)

    time.sleep(1)
    #返回
    while True:
        location = pyautogui.locateCenterOnScreen("raca/return2.png", confidence=0.9)
        if location is not None:
          print("返回按钮图片2 - 游戏结束",location.x, location.y)
          pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2, button="left")
          break
        print("未找到返回按钮图片2 - 游戏结束,0.1秒后重试")
        time.sleep(0.1)

  print("游戏结束")

